Below is a C program which takes two matrix form users to multiply them and print the results on the screen.
Product of Two Matrices Using C
Here is the source code of the program in C to Compute the Product of Two Matrices. This program is successfully compiled, and the output is also given below.
#include <stdio.h> int main() { int row1, clum1, row2, clum2; int i, j, k; int A[8][8], B[8][8], C[8][8]; printf("Please Enter Rows and Columns Of Mateix A : "); scanf("%d%d", & row1, & clum1); printf("\nPlease Enter Rows and Columns of Matrix B : "); scanf("%d%d", & row2, & clum2); if (clum1 != row2) { printf("\nNumber of Columns of A is not Equal to Number of Rows of Y "); scanf("\nso, It is not Possible to Multily Both matrixs "); } else { printf("\nPlease Enter Elements of A Matrix : "); for (i = 0; i < row1; i++) { for (j = 0; j < clum1; j++) { scanf("%d", & A[i][j]); } } printf("\nEnter elements of matrix B : "); for (i = 0; i < row2; i++) { for (j = 0; j < clum2; j++) { scanf("%d", & B[i][j]); } } for (i = 0; i < row1; i++) { for (j = 0; j < clum2; j++) { C[i][j] = 0; for (k = 0; k < row2; k++) { C[i][j] += A[i][k] * B[k][j]; } } } printf("\nMultiplication Of Matrix :\n "); for (i = 0; i < row1; i++) { for (j = 0; j < clum2; j++) { printf("%d\t", C[i][j]); } printf("\n"); } } return 0; }
Output
![](..\wp-content\uploads\2019\05\matrix.png)
Compute the Product of Two Matrices Using C
Program Explanation
The program starts execution from the main
method, taking the number of row and columns of matrices to form the user. If the number of columns of matrix A is not equal to the number of rows of matrix B, then multiplication is not possible. Else, use nested for-loops
to read the elements of matrices. Again, use nested for loop to multiply both matrices. And save the multiplied items in C-Array. After multiplication is done, print the Array C which is the multiplication of matrix A and B.